package com.copa.l938;

/**
 * @author copa
 * @createDate 2022-06-30 17:59
 * @function DFS
 */
public class Test1 {

    public int rangeSumBST(TreeNode root, int low, int high) {
        int result = 0;
        while (root != null) {
            if (root.val >= low && root.val <= high) {
                result += root.val;
            }
            if (root.left != null) {
                result += rangeSumBST(root.left, low, high);
            }
            if (root.right != null) {
                result += rangeSumBST(root.right, low, high);
            }
            break;
        }
        return result;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(10);
        root.left = new TreeNode(5);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(7);
        root.right = new TreeNode(15);
        root.right.right = new TreeNode(18);
        System.out.println(new Test1().rangeSumBST(root, 7, 15));
    }

}
